package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.Chat;
import cn.wolfcode.trip.base.query.ChatQuery;
import cn.wolfcode.trip.base.service.IChatService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("chats")
public class ChatController {

    @Autowired
    private IChatService chatService;

    /**
     * 查询消息列表
     * 1.资源 /chats
     * 2.请求方式 get
     * 3.请求参数 ChatQuery
     * 4.返回结果 PageInfo
     */
    @GetMapping
    public PageInfo listChat(ChatQuery qo){
        qo.setPageSize(10);
        return chatService.query(qo);
    }

    /**
     * 修改消息状态
     * 1.资源 /chats/updateState
     * 2.请求方式 put
     * 3.请求参数 id, state
     * 4.返回结果 JsonResult
     */
    @PutMapping("updateState")
    public JsonResult update(Long id, Integer state){
        JsonResult result = new JsonResult();
        try {
            chatService.updateState(id, state);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 获取聊天记录
     * 1.资源 /chats/updateState
     * 2.请求方式 put
     * 3.请求参数 id, state
     * 4.返回结果 JsonResult
     */
    @GetMapping("chatHistory")
    public List<Chat> chatHistory(Long sendUserId, Long receiveUserId){
        return chatService.chatHistory(sendUserId, receiveUserId);
    }
}
